package cn.ibdsr.human.service.impl;

import cn.ibdsr.human.constant.*;
import cn.ibdsr.human.db.model.*;
import cn.ibdsr.human.db.mapper.NotificationMapper;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.service.INotificationService;
import cn.ibdsr.human.transfer.company.CompanyResponseDTO;
import cn.ibdsr.human.transfer.companyrecommend.CompanyRecommendImgDTO;
import cn.ibdsr.human.transfer.job.CompanyJobDTO;
import cn.ibdsr.human.transfer.job.JobCompanyResponseDTO;
import cn.ibdsr.human.transfer.job.JobOperatorDTO;
import cn.ibdsr.human.transfer.job.JobUserDTO;
import cn.ibdsr.human.transfer.notification.NotificaitonQueryDTO;
import cn.ibdsr.human.transfer.notification.NotificationDTO;
import cn.ibdsr.human.transfer.notification.NotificationListDTO;
import cn.ibdsr.human.util.DateUtils;
import cn.ibdsr.human.util.MybatisPlusPage;
import cn.ibdsr.human.warpper.CompanyResponseWarpper;
import cn.ibdsr.human.warpper.JobResponseWarpper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author cdl
 * @since 2022-03-26
 */
@Service
public class NotificationServiceImpl implements INotificationService {

    @Resource
    NotificationMapper notificationMapper;

    @Override
    public NotificationDTO getNotification(Long id) {
        NotificationDTO notificationDTO = new NotificationDTO();
        Notification notification = notificationMapper.selectById(id);
        if (notification == null) {
            throw new BusinessException(BizExceptionEnum.NOTIFICATION_NOT_EXISTED);
        }
        notificationDTO = JSONObject.parseObject(JSONObject.toJSONString(notification), NotificationDTO.class);
        notificationDTO.setPubListStr(PublishEnum.valueOf(notification.getIsPublish()).getMessage());
        notificationDTO.setDeleteDes(DeleteEnum.valueOf(notification.getIsPublish()).getDesc());
        notificationDTO.setUpdateTimeStr(notification.getUpdateTime().format(DateUtils.FORMATTER_DATETIME));
        if (!StringUtils.isEmpty(notificationDTO.getUrls())) {
            List<String> jsonSTR = JSONArray.parseArray(notificationDTO.getUrls()).toJavaList(String.class);
            notificationDTO.setUrlList(jsonSTR);
        }
        return notificationDTO;
    }


    @Override
    public NotificationListDTO getNotifications(NotificaitonQueryDTO notificaitonQueryDTO) {
        if (notificaitonQueryDTO.getPageNo() == null || notificaitonQueryDTO.getPageNo() <= 0) {
            notificaitonQueryDTO.setPageNo(1L);
        }
        if (notificaitonQueryDTO.getPageSize() == null || notificaitonQueryDTO.getPageSize() <= 0) {
            notificaitonQueryDTO.setPageSize(BizConstants.DEFAULT_PAGE_SIZE);
        } else if (notificaitonQueryDTO.getPageSize() > BizConstants.MAX_PAGE_SIZE) {
            throw new BusinessException(BizExceptionEnum.REQUEST_ERROR);
        }

        Page<NotificationDTO> page = MybatisPlusPage.defaultNewPage(notificaitonQueryDTO);
        List<NotificationDTO> notificationDTOS = notificationMapper.listNoticafition(page,notificaitonQueryDTO.getIsPublish());


        List<NotificationListDTO> notificationListDTOS = new ArrayList<>();
        // 创建返回结果
        for (NotificationDTO notificationDTO : notificationDTOS) {
            notificationDTO.setPubListStr(PublishEnum.valueOf(notificationDTO.getIsPublish()).getMessage());
            notificationDTO.setDeleteDes(DeleteEnum.valueOf(notificationDTO.getIsPublish()).getDesc());
            notificationDTO.setUpdateTimeStr(notificationDTO.getUpdateTime().format(DateUtils.FORMATTER_DATETIME));
            if (!StringUtils.isEmpty(notificationDTO.getUrls())) {
                List<String> jsonSTR = JSONArray.parseArray(notificationDTO.getUrls()).toJavaList(String.class);
                notificationDTO.setUrlList(jsonSTR);
            }

        }
        page.setRecords(notificationDTOS);
        NotificationListDTO notificationListDTO = new NotificationListDTO();
        notificationListDTO.setPageNo(page.getCurrent());
        notificationListDTO.setPageSize(page.getSize());
        notificationListDTO.setTotalPage(page.getPages());
        notificationListDTO.setTotalCount(page.getTotal());
        notificationListDTO.setData(page.getRecords());
        return notificationListDTO;
    }

    @Override
    public Long save(Long userId, NotificationDTO notificationDTO) {
        //  return null;
        Notification notification = notificationMapper.selectById(notificationDTO.getId());
        if (notification == null) {
            throw new BusinessException(BizExceptionEnum.NOTIFICATION_NOT_EXISTED);
        }
        notification = new Notification();
        notification = JSONObject.parseObject(JSONObject.toJSONString(notificationDTO), Notification.class);
        if (!StringUtils.isEmpty(notificationDTO.getUrlList()) && notificationDTO.getUrlList().size() > 0) {
            notification.setUrls(JSONObject.toJSONString(notificationDTO.getUrlList()));
        }
        notification.updateById();
        return notification.getId();
    }

    @Override
    public Long create(Long userId, NotificationDTO notificationDTO) {
        Notification notification = new Notification();
        notification = JSONObject.parseObject(JSONObject.toJSONString(notificationDTO), Notification.class);
        notification.setCreateUser(userId);
        if (!StringUtils.isEmpty(notificationDTO.getUrlList()) && notificationDTO.getUrlList().size() > 0) {
            notification.setUrls(JSONObject.toJSONString(notificationDTO.getUrlList()));
        }
        notificationMapper.insert(notification);
        return notification.getId();
    }

    @Override
    public Long publishOrNot(Long userId, JobOperatorDTO jobSaveDTO) {
        Notification notification = notificationMapper.selectById(jobSaveDTO.getId());
        if (notification == null) {
            throw new BusinessException(BizExceptionEnum.NOTIFICATION_NOT_EXISTED);
        }
        notification.setIsPublish(jobSaveDTO.getIsPublish() ? 1 : 0);
        notification.updateById();
        return notification.getId();
    }

    @Override
    public Long delete(Long userId, Long id) {
        Notification notification = notificationMapper.selectById(id);
        if (notification == null) {
            throw new BusinessException(BizExceptionEnum.NOTIFICATION_NOT_EXISTED);
        }
        notification.setIsDeleted(1);
        notification.updateById();
        return notification.getId();
    }

}
